// pages/luggage/add.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "",
    weekArray: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    week: 0,
    wordNum: 0,
    notepad: "",
    weather: ""
  },
  changeDate(e) {
    this.setData({
      date: e.detail.value
    });
  },
  bindWeekChange(e) {
    this.setData({
      week: e.detail.value
    })
  },
  bindNotepadInput(e) {
    this.setData({
      notepad: e.detail.value,
      wordNum: e.detail.value.length
    })
  },
  bindWeatherInput(e) {
    this.setData({
      weather: e.detail.value,
    })
  },
  formatNumber(a) {
    a = a.toString()
    return a[1] ? a : '0' + a
  },
  getNowData() {
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return [year, month, day].map(this.formatNumber).join('-')
  },
  addData() {
    // wx.showModal({
    //   title: '系统提示',
    //   content: '您确认要现在添加吗？',
    //   confirmText: "添加",
    //   success(res) {
        
    //   }
    // })
    wx.request({
      url: app.globalData.requestUrl + 'api/luggage/add.php',
      method: "post",
      data: {
        date: this.data.date,
        week: this.data.week,
        weather: this.data.weather,
        notepad: this.data.notepad
      },
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync("token")
      },
      success: (res) => {
        if (res.data.code === 0) {
          this.setData({
            weather: "",
            notepad: ""
          })
          wx.redirectTo({
            url: '../luggage',
          })
        } else if (res.data.code == 1010) {
          wx.showModal({
            title: '请登录',
            content: '您未登录或登录状态已过期',
            showCancel: false,
            confirmText: "去登录",
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../../index/index',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '格式错误',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail: (res) => {
        wx.showModal({
          title: '提示',
          content: '服务器开小差了，请稍后重试',
          showCancel: false,
          confirmText: "我知道了"
        })
      }
    })
  },
  /* 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const nowDate = this.getNowData()
    const week = new Date().getDay()
    this.setData({
      date: nowDate,
      week
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})